package com.hs.ph.Servlet;

import com.alibaba.fastjson.JSON;
import com.hs.ph.Dao.Impl.yzglDaoimpl;
import com.hs.ph.Dao.YzglDao;
import com.hs.ph.Entity.Result;
import com.hs.ph.Entity.Yzgl;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/yzgl")
public class YzglServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action=req.getParameter("action");
        YzglDao yzglDao=new yzglDaoimpl();
        if ("selectAll".equals(action)){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String courierStationName = req.getParameter("courierStationName");
            String communityName = req.getParameter("communityName");
//            String courierStationStatus = req.getParameter("courierStationStatus");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            //封装到map
            Map<String,String> map=new HashMap();
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("courierStationName",courierStationName);
            map.put("communityName",communityName);
//            map.put("courierStationStatus",courierStationStatus);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);

            //连接数据库
            List<Yzgl> list = yzglDao.selectAll(map);
            //查询总条数
            int total = yzglDao.selectCount(map);
            //响应结果
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)) {
            Date addTime=new Date();
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String a = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId=0;
            if (a!=null&&!"".equals(a)){
                courierStationCommunityId=Integer.parseInt(a);
            }
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String Id = req.getParameter("courierStationId");
            int courierStationId=0;
            if (Id!=null&&!"".equals(Id)){
               courierStationId=Integer.parseInt(Id);
            }
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String aaaa = req.getParameter("courierStationOrderCount");
            int courierStationOrderCount=0;
            if (aaaa!=null&&!"".equals(aaaa)){
                courierStationOrderCount=Integer.parseInt(aaaa);
            }
            String courierStationProvince = req.getParameter("courierStationProvince");
            String aaa = req.getParameter("courierStationSort");
            int courierStationSort=0;
            if (aaa!=null&&!"".equals(aaa)){
                courierStationSort=Integer.parseInt(aaa);
            }
            String aa = req.getParameter("courierStationStatus");
            int courierStationStatus=0;
            if (aa!=null&&!"".equals(aa)){
                courierStationStatus=Integer.parseInt(aa);
            }
            Yzgl yzgl =new Yzgl(addTime,courierStationAddress,courierStationCity,courierStationCommunityId,courierStationCommunityName,courierStationDistrict,courierStationId,courierStationImage,courierStationName,courierStationOrderCount,courierStationProvince,courierStationSort,courierStationStatus);
                int num= yzglDao.insert(yzgl);
                Result result=null;
                if (num!=0){
                    result=Result.success("新增成功");
                }else {
                    result=Result.error("新增失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
        }else if ("date".equals(action)){
            Date addTime=new Date();
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String a = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId=0;
            if (a!=null&&!"".equals(a)){
                courierStationCommunityId=Integer.parseInt(a);
            }
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String Id = req.getParameter("courierStationId");
            int courierStationId=0;
            if (Id!=null&&!"".equals(Id)){
                courierStationId=Integer.parseInt(Id);
            }
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String aaaa = req.getParameter("courierStationOrderCount");
            int courierStationOrderCount=0;
            if (aaaa!=null&&!"".equals(aaaa)){
                courierStationOrderCount=Integer.parseInt(aaaa);
            }
            String courierStationProvince = req.getParameter("courierStationProvince");
            String aaa = req.getParameter("courierStationSort");
            int courierStationSort=0;
            if (aaa!=null&&!"".equals(aaa)){
                courierStationSort=Integer.parseInt(aaa);
            }
            String aa = req.getParameter("courierStationStatus");
            int courierStationStatus=0;
            if (aa!=null&&!"".equals(aa)){
                courierStationStatus=Integer.parseInt(aa);
            }
            Yzgl yzgl =new Yzgl(addTime,courierStationAddress,courierStationCity,courierStationCommunityId,courierStationCommunityName,courierStationDistrict,courierStationId,courierStationImage,courierStationName,courierStationOrderCount,courierStationProvince,courierStationSort,courierStationStatus);
            int num= yzglDao.update(yzgl);
            Result result=null;
            if (num!=0){
                result=Result.success("修改成功");
            }else {
                result=Result.error("修改失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("te".equals(action)) {
            String goodsId = req.getParameter("courierStationId");
            String goodsStatus = req.getParameter("courierStationStatus");
            YzglDao yzglDao1=new yzglDaoimpl();
            yzglDao1.update1(goodsId,goodsStatus);
        }
    }
}
